<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
             
                <el-form-item label="${column.comments}" prop="operation">
            <el-input v-model="dataForm.operation" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="requestUri">
            <el-input v-model="dataForm.requestUri" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="requestMethod">
            <el-input v-model="dataForm.requestMethod" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="requestParams">
            <el-input v-model="dataForm.requestParams" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="requestTime">
            <el-input v-model="dataForm.requestTime" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="userAgent">
            <el-input v-model="dataForm.userAgent" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="ip">
            <el-input v-model="dataForm.ip" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="status">
            <el-input v-model="dataForm.status" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="creatorName">
            <el-input v-model="dataForm.creatorName" placeholder="${column.comments}"></el-input>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {getGoperationById,putGoperation,postGoperation} from "@/network/lswms/logs/Goperation"
import {ElMessage} from 'element-plus'
import _ from 'lodash'


export default {
  name: "goperationAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    const dataForm = ref({
		          id: '',
          operation: '',
          requestUri: '',
          requestMethod: '',
          requestParams: '',
          requestTime: '',
          userAgent: '',
          ip: '',
          status: '',
          creatorName: '',
          creator: '',
          createDate: ''
    });


    const dataRule = reactive({   
                operation: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          requestUri: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          requestMethod: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          requestParams: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          requestTime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          userAgent: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          ip: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          status: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          creatorName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],    })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        if (dataForm.value.id) {
		getgoperationInfo();
		}
        
      })
    }

	const getgoperationInfo =()=>{
		getGoperationById(dataForm.value.id).then(function (res) {
		  if (res.code !== 0) {
		    return ElMessage.error(res.msg);
		  }
		  dataForm.value = _.assignIn({}, dataForm.value, res.data);
		}).catch((err) =>{
		});
	}

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }
		
        let qing = ()=> {
          return !dataForm.value.id ?  postGoperation(dataForm.value) : putGoperation(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      dataForm,
      dataRule,
      init,
      dataFormSubmitHandle,
      dataFormRef

    }
  },


}
</script>

<style scoped>

</style>